from django.http import JsonResponse
from drf_spectacular.utils import extend_schema,inline_serializer
from rest_framework.decorators import action
from rest_framework.mixins import (CreateModelMixin, ListModelMixin,
                                   RetrieveModelMixin, UpdateModelMixin,
                                   DestroyModelMixin
                                   )
from rest_framework.viewsets import GenericViewSet
from backend.cews.services.VersionService import soft_delete_version_master
from utils.autoschema import AutoDocSchema, custom_response_serailizer
from utils.decorators import error_response, request_data
from backend.cews.models import RepairOrder, RepairOrderEDoc, StockItem, StockItemEDoc, VersionMaster
from .serializers import (RepairOrderEDocSerializer, StockItemEDocSerializer, VersionCurrentLatestSerailizer, VersionMasterSerializer)
from rest_framework.response import Response
from django_filters import rest_framework as filters
import django_filters

class CustomFilter(filters.FilterSet):
    stock_code = filters.Filter(field_name='stock_code', lookup_expr='exact')
    class Meta:
        model = VersionMaster
        fields = ['stock_code']
        

class VersionMasterViewSet(
    RetrieveModelMixin,
    ListModelMixin,
    UpdateModelMixin,
    GenericViewSet,
    CreateModelMixin,
    DestroyModelMixin
):
    serializer_class = VersionMasterSerializer
    queryset = VersionMaster.objects.filter(deleted=False)
    filterset_class = CustomFilter
    filter_backends = [django_filters.rest_framework.DjangoFilterBackend]
    
    @action(
        detail=False, methods=["DELETE"],
        url_name="soft_delete_version_master", schema=AutoDocSchema()
    )
    @request_data(
        keys=[
            "id"
        ]
    )
    @error_response()
    def soft_delete_version_master(
        self,
        request,
        id
    ):

        if soft_delete_version_master(
            id,
            request.user
        ):
            response = {
                "code": 1,
                "message": "Success delete part"
            }

        return Response(status=200, data=response)